import React from 'react'
// ===================================================================== Temp
import SearchForm from '#cpt/temp/SearchForm'
import Controls from '#cpt/temp/Controls'
import Table from '#cpt/temp/Table'
import TableControls from '#cpt/temp/TableControls'
// ===================================================================== Declare
const { $fn, $Lazy } = window
// ===================================================================== Lazy
const $cache 			= $Lazy.file(()=>import('#private/cache-api'))
const HospitalModal 	= $Lazy.state(()=>import('./hospital-modal'))
// ===================================================================== component
class Index extends React.Component{
	state = {
		data: [],
		pag: {},
		selectedKeys: [],
		forms : [
			{ label: '医院名称',		name: 'hosp_id',	type: 'select',	loading:true,	nameStr:'name',	onValue: ({rows})=>{ this.model.hosp_name=rows?.name } },
			{ label: '医院性质',		name: 'nature',		type: 'select', loading:true,	idStr:'code',	onValue: ({rows})=>{ this.model.nature_name=rows?.name } },
			{ label: '医院代码',		name: 'hosp_code' },
			{ label: '医院条码',		name: 'hosp_spec' },
			{ label: '医院级别',		name: 'level',		type: 'select', loading:true,	idStr:'code' }
		]
	}
	query = {}
	model = {}
	componentDidMount(){
		this.getSelect()
	}
	
	getHospital = (uuid) => {
		this.query = {act_id:uuid}
		this.fetch()
	}
	
	// 获取下拉数据
	getSelect = () => {
		const { forms } = this.state
		$cache.then( async f => {
			// 医院名称
			$fn.setRow(forms, 'hosp_id', await f.getCache({ api:'hospital' } ) )
			//医院性质
			$fn.setRow(forms, 'nature', await f.getDicCache({ code:44100 } ) )
			//医院级别
			$fn.setRow(forms, 'level', await f.getDicCache({ code:39000 } ) )
			//
			this.setState({ forms })
		})
	}
	
	// paging
	fetch = () => $fn.fetch.call(this, 'act-conf-marketing/hospLists',)
	
	// 表单
	cols = [
		{ title: '医院代码',		dataIndex: 'hosp_code', 	...$fn.table.hosp_code },
		{ title: '医院条码', 	dataIndex: 'hosp_spec', 	...$fn.table.num },
		{ title: '医院名称', 	dataIndex: 'hosp_name', 	width:200 },
		{ title: '医院性质', 	dataIndex: 'nature_name', 	width:100 },
		{ title: '医院级别',		dataIndex: 'level_name', 	width:100 },
		TableControls.controls.call(this, { width: 60 })
	]
	
	table_controls = (text, { uuid }) => [
		Controls.submit.call(this, '删除', 'act-conf-marketing/unbindhosp', { param: {act_id: this.query.act_id, hosp_ids:[uuid]},onRefresh:this.fetch} ),
	]
	
	controls = () => {
		return [
			Controls.click.call(this,'添加',()=> this.hospitalModal.open(this.query.act_id),{disabled:$fn.isEmpty(this.query.act_id)}),
			Controls.submit.call(this,'删除','act-conf-marketing/unbindhosp',{param:{act_id:this.query.act_id,hosp_ids:this.state.selectedKeys},isKeys:true,onRefresh:this.fetch}),
			Controls.click.call(this,'搜索',()=> this.fetch(),{disabled:$fn.isEmpty(this.query.act_id)})
		]
	}
	
	render(){
		return (
		<>
			{/* 搜索 */}
			{ SearchForm.call(this, {noRefresh:true, noReset:true, controls: this.controls()})}
			{/* 表格 */}
			{ Table.call(this )}
			<HospitalModal onRef={ref=>this.hospitalModal=ref } onRefresh={this.fetch} />
		</>
		)

	}
}

export default Index